﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Collections.Generic;

namespace VirtualTournaments.Frontend.MenuControls.GamblingManagerMenu.InnerControls
{
    using TournamentsService = Services.Tournaments.TournamentsService;
    using Tournament = Services.Tournaments.Tournament;
    using Match = Services.Tournaments.Match;
    using User = Services.Users.User;
    using UsersService = Services.Users.UsersService;
    using Journey = Services.Tournaments.Journey;
    using MatchResultEnum = Services.Bets.MatchResultEnum;
    using BetsService = Services.Bets.BetsService;
    
    public partial class MatchBet : System.Web.UI.UserControl
    {
        public EventHandler GoToTeamInfoButton;
        public EventHandler GoToCancelBetButton;

        public string TournamentName
        {
            get
            {
                return TournamentNameLabel.Text;
            }
            set 
            {
                TournamentNameLabel.Text = value;
            }
        }
        
        public ListItemCollection JourneySelectionRadios
        {
            get 
            {
                return JourneySelectionRadioButtonList.Items;
            }
            private set { }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            
            if (Session["CurrentHardcodedJourneyIndex"] == null)
                Session.Add("CurrentHardcodedJourneyIndex", 0);

            int journeyIndex = (int)Session["CurrentHardcodedJourneyIndex"];

            if (Session["HardcodedJourneys"] != null)
            {
                Journey currentJourney = ((List<Journey>)Session["HardcodedJourneys"])[journeyIndex];
                if (Session["CurrentJourneyMatches"] == null)
                    Session.Add("CurrentJourneyMatches", currentJourney);
                else
                    Session["CurrentJourneyMatches"] = currentJourney;
            }

            this.JourneyManagerControl.FontSize = FontUnit.Larger;
        }

        protected void TeamInfoButton_OnClick(object sender, EventArgs e)
        {
            //TODO: pick the team!
            if (this.JourneySelectionRadioButtonList.SelectedIndex >= 0)
                GoToTeamInfoButton(this, new VirtualTournamentsFrontendEvent());

        }

        protected void AcceptBetButton_OnClick(object sender, EventArgs e)
        {
            //TODO: validate amount!!!
            if ( (JourneySelectionRadioButtonList.SelectedIndex >=0) && (MatchResultRadioButtonList.SelectedIndex >= 0) )
            {
                int betMatchIndex;
                if (JourneySelectionRadioButtonList.SelectedItem.Text.Equals("1"))
                    betMatchIndex = 0;
                else
                    betMatchIndex = 1;
                Journey journey = (Journey)Session["CurrentJourneyMatches"];
                Match betMatch = journey.Matches[betMatchIndex];

                BetInfoLabel.Text = "Match: " + betMatch.Local + " vs. " + betMatch.Visitor + "<br/>";
                BetInfoLabel.Text += "Match result:  ";
                if (MatchResultRadioButtonList.SelectedItem.Text.Equals("Draw"))
                    BetInfoLabel.Text += "Draw";
                else
                    BetInfoLabel.Text += "Winner:    " + MatchResultRadioButtonList.SelectedItem.Text;
                
                BetInfoLabel.Text += "<br/ >";
                BetInfoLabel.Text += "Bet amount: u$s " + AmountTextBox.Text;

                pagedimmer.Visible = true;
                msgbox.Visible = true;
            }
        }
        protected void ResetBetButton_OnClick(object sender, EventArgs e)
        {
            //if (!FirstPlaceTeam.Text.Equals("Team: "))
            //    TeamsListBox.Items.Add(FirstPlaceTeam.Text);
            //if (!SecondPlaceTeam.Text.Equals("Team: "))
            //    TeamsListBox.Items.Add(SecondPlaceTeam.Text);
            //if (!ThirdPlaceTeam.Text.Equals("Team: "))
            //    TeamsListBox.Items.Add(ThirdPlaceTeam.Text);

            //FirstPlaceTeam.Text = SecondPlaceTeam.Text = ThirdPlaceTeam.Text = "Team: ";
            AmountTextBox.Text = "";
        }
        protected void CancelBetButton_OnClick(object sender, EventArgs e)
        {
            GoToCancelBetButton(this, new VirtualTournamentsFrontendEvent());
        }

        protected void MatchBetGoBackButton_OnClick(object sender, EventArgs e)
        {
            //TODO: left this way because the effect is the same of CancelBetButton_OnClick          
            GoToCancelBetButton(this, new VirtualTournamentsFrontendEvent());
        }

        protected void FinishCurrentBetButton_OnClick(object sender, EventArgs e)
        {
            pagedimmer.Visible = false;
            msgbox.Visible = false;
            FinishCurrentBetButton.Enabled = false;
            ConfirmCurrentBetButton.Enabled = true;
            CancelCurrentBetButton.Enabled = true;
            StatusLabel.Text = "Waiting for confirmation...";
            
            //TODO: left this way because the effect is the same of CancelBetButton_OnClick          
            GoToCancelBetButton(this, new VirtualTournamentsFrontendEvent());
        }

        protected void ConfirmCurrentBetButton_OnClick(object sender, EventArgs e)
        {
            //TODO: process bet!
            Tournament tournament = TournamentsService.GetTournamentByName(TournamentName);
            int betMatchIndex = JourneySelectionRadioButtonList.SelectedIndex;
            Journey journey = (Journey)Session["CurrentJourneyMatches"];
            Match betMatch = journey.Matches[betMatchIndex];
            User user = Session["CurrentUser"] as User;
            MatchResultEnum matchResult;
            
            if (MatchResultRadioButtonList.SelectedValue.Equals("Home"))
                matchResult = MatchResultEnum.Home;
            else if (MatchResultRadioButtonList.SelectedValue.Equals("Away"))
                matchResult = MatchResultEnum.Away;
            else
                matchResult = MatchResultEnum.Draw;

            //BetsService.BetMatch(tournament, betMatch, user, Convert.ToDecimal(AmountTextBox.Text), matchResult);
            
            StatusLabel.Text = "Your bet has been registered! Click 'Finish' to complete the operation.";
            FinishCurrentBetButton.Enabled = true;
            ConfirmCurrentBetButton.Enabled = false;
            CancelCurrentBetButton.Enabled = false;
        }

        protected void CancelCurrentBetButton_OnClick(object sender, EventArgs e)
        {
            pagedimmer.Visible = false;
            msgbox.Visible = false;
        }

        protected override void OnUnload(EventArgs e)
        {
            base.OnUnload(e);

            AmountTextBox.Text = "";

        }
    }
}